package org.codert.web;

import org.codert.domain.OrderRepository;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author: TanJingyu
 * @create: 2021-05-17 17:54
 **/
@RestController
@RequestMapping("/order")
public class OrderController {
    private final OrderRepository repository;

    public OrderController(OrderRepository repository) {
        this.repository = repository;
    }

    @GetMapping
    public Object get() {
        return repository.findAll();
    }

    @GetMapping("/test")
    public String test(@RequestParam(required = false, defaultValue = "a,b,c") List<String> ids) {
        return String.join("-", ids);
    }

    @GetMapping("{id}")
    public String id(@PathVariable String id) {
        if ("1".equals(id)) {
            throw new NoSuchElementException("异常。。。。");
        }
        return id;
    }

    @ExceptionHandler(Exception.class)
    public String hh(Exception e) {
        return e.getMessage();
    }
}
